<template>
  <van-nav-bar
    left-text="返回"
    left-arrow
    @click-left="$router.back()"
    right-text="注册"
    title="登录"
    @click-right="$router.push('/register')"
    placeholder
    fixed
  />
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
        required
        v-model="userName"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        required
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px">
      <van-button round block type="primary" native-type="submit"> 登录 </van-button>
    </div>
  </van-form>
</template>

<script setup>
import { userLoginReq } from '@/api/user'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
let userName = ref('')
let password = ref('')
let router = useRouter()
function onSubmit() {
  // console.log('点击了')
  userLoginReq(userName.value, password.value).then((res) => {
    if (res.data.code === 1) {
      // eslint-disable-next-line no-undef
      showSuccessToast('登录成功')
      localStorage.setItem('mall-token', res.data.data)
      router.push('/')
    } else {
      // eslint-disable-next-line no-undef
      showFailToast(res.data.data)
    }
    // console.log(res.data)
  })
}
</script>

<style scoped></style>
